package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduDepartmentMapper;
import com.zenith.edu.po.domain.EduDepartment;
import com.zenith.edu.po.dto.EduDepartmentDTO;
import com.zenith.edu.po.vo.EduDepartmentVO;
import com.zenith.edu.service.EduDepartmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduDepartmentServiceImpl extends ServiceImpl<EduDepartmentMapper, EduDepartment> implements EduDepartmentService {
    
    @Override
    public EduDepartmentVO createDepartment(EduDepartmentDTO dto) {
        EduDepartment department = new EduDepartment();
        BeanUtils.copyProperties(dto, department);
        this.save(department);
        
        EduDepartmentVO vo = new EduDepartmentVO();
        BeanUtils.copyProperties(department, vo);
        return vo;
    }
    
    @Override
    public EduDepartmentVO getDepartmentById(Long id) {
        EduDepartment department = this.getById(id);
        if (department == null) {
            return null;
        }
        
        EduDepartmentVO vo = new EduDepartmentVO();
        BeanUtils.copyProperties(department, vo);
        return vo;
    }
    
    @Override
    public EduDepartmentVO updateDepartment(EduDepartmentDTO dto) {
        EduDepartment department = new EduDepartment();
        BeanUtils.copyProperties(dto, department);
        this.updateById(department);
        
        EduDepartmentVO vo = new EduDepartmentVO();
        BeanUtils.copyProperties(department, vo);
        return vo;
    }
    
    @Override
    public boolean deleteDepartment(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduDepartmentVO> getDepartmentList() {
        List<EduDepartment> departmentList = this.list();
        return departmentList.stream().map(department -> {
            EduDepartmentVO vo = new EduDepartmentVO();
            BeanUtils.copyProperties(department, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}